# 数据库环境配置
# 包含 MongoDB、Redis、MySQL 等

# ========== MongoDB 配置 ==========
# MongoDB 路径
export PATH="/usr/local/opt/mongodb-community/bin:$PATH"
export MONGO_HOME="/usr/local/opt/mongodb-community"

# MongoDB 数据目录
export MONGO_DATA="$HOME/data/mongodb"
export MONGO_LOG="$HOME/logs/mongodb"
export MONGO_CONF="$HOME/data/mongodb/mongod.conf"

# MongoDB 启停函数
function mongo-start() {
  if pgrep -x "mongod" > /dev/null; then
    log_info "MongoDB 已运行"
  else
    log_info "启动 MongoDB 服务..."
    # 确保日志目录存在
    mkdir -p "$MONGO_LOG"
    # 使用配置文件(里面已包含fork:true)或直接使用命令行参数
    mongod --config "$MONGO_CONF" || mongod --dbpath "$MONGO_DATA" --fork --logpath "$MONGO_LOG/mongod.log"
    log_success "MongoDB 已启动"
  fi
}

function mongo-stop() {
  if pgrep -x "mongod" > /dev/null; then
    log_info "停止 MongoDB 服务..."
    # 尝试使用admin命令停止，如果失败则使用kill信号
    mongod --dbpath "$MONGO_DATA" --shutdown 2>/dev/null || pkill -15 mongod || pkill -9 mongod
    log_success "MongoDB 已停止"
  else
    log_info "MongoDB 未运行"
  fi
}

function mongo-status() {
  if pgrep -x "mongod" > /dev/null; then
    log_running "MongoDB"
    ps aux | grep -i mongo | grep -v grep
  else
    log_stopped "MongoDB"
  fi
}

function mongo-restart() {
  mongo-stop
  sleep 2
  mongo-start
}

# ========== Redis 配置 ==========
# Redis 路径
export PATH="/usr/local/opt/redis/bin:$PATH"

# Redis 启停函数
function redis-start() {
  if pgrep "redis-server" > /dev/null; then
    log_info "Redis 已运行"
  else
    log_info "启动 Redis 服务..."
    redis-server --daemonize yes
    log_success "Redis 已启动"
  fi
}

function redis-stop() {
  if pgrep "redis-server" > /dev/null; then
    log_info "停止 Redis 服务..."
    redis-cli shutdown
    log_success "Redis 已停止"
  else
    log_info "Redis 未运行"
  fi
}

function redis-status() {
  if pgrep "redis-server" > /dev/null; then
    log_running "Redis"
    ps aux | grep -i redis | grep -v grep
    log_subheader "Redis 信息"
    redis-cli info | grep -E 'redis_version|connected|memory|db0'
  else
    log_stopped "Redis"
  fi
}

# ========== MySQL 配置 ==========
# MySQL 路径
export PATH="/usr/local/mysql/bin:$PATH"
# MySQL 密码: sun123123 (注释在这里以防忘记)

# MySQL 启停函数
function mysql-start() {
  if pgrep -x "mysqld" > /dev/null; then
    log_info "MySQL 已运行"
  else
    log_info "启动 MySQL 服务..."
    sudo /usr/local/mysql/support-files/mysql.server start
    log_success "MySQL 已启动"
  fi
}

function mysql-stop() {
  if pgrep -x "mysqld" > /dev/null; then
    log_info "停止 MySQL 服务..."
    sudo /usr/local/mysql/support-files/mysql.server stop
    log_success "MySQL 已停止"
  else
    log_info "MySQL 未运行"
  fi
}

function mysql-status() {
  if pgrep -x "mysqld" > /dev/null; then
    log_running "MySQL"
    ps aux | grep -i mysql | grep -v grep
  else
    log_stopped "MySQL"
  fi
}

function mysql-restart() {
  if pgrep -x "mysqld" > /dev/null; then
    log_info "重启 MySQL 服务..."
    sudo /usr/local/mysql/support-files/mysql.server restart
    log_success "MySQL 已重启"
  else
    log_info "MySQL 未运行，现在启动..."
    mysql-start
  fi
}

# ========== 常用别名 ==========
# MongoDB
alias mdb="mongo"
alias mdbs="mongo-status"
alias mdbc="mongosh"

# Redis
alias rds="redis-cli"
alias rdss="redis-status"

# MySQL
alias msq="mysql -u root -p"  # 需要输入密码
alias msqa="mysql -u root -psun123123"  # 自动使用密码(不安全但方便)
alias msqs="mysql-status"

# 创建MySQL安全配置文件(一次性执行)
function mysql-setup-config() {
  log_info "创建MySQL配置文件以保存凭据..."
  mkdir -p ~/.mysql
  echo "[client]
user=root
password=sun123123" > ~/.mysql/my.cnf
  chmod 600 ~/.mysql/my.cnf
  log_success "配置文件已创建: ~/.mysql/my.cnf (权限设置为仅当前用户可读)"
}

# 使用配置文件连接MySQL(推荐使用这种方法)
alias msqc="mysql --defaults-file=~/.mysql/my.cnf"

# 数据库状态检查
function check_db_status() {
  log_header "数据库服务状态"
  log_subheader "MongoDB"
  mongo-status
  log_subheader "Redis"
  redis-status
  log_subheader "MySQL"
  mysql-status
}

alias db-status="check_db_status"
alias db-paths="log_db_paths"

# 数据库服务启动
function start_all_db() {
  log_header "启动所有数据库服务"
  mongo-start
  redis-start
  mysql-start
  check_db_status
}

alias db-start="start_all_db"

# 数据库服务停止
function stop_all_db() {
  log_header "停止所有数据库服务"
  mongo-stop
  redis-stop
  mysql-stop
  check_db_status
}

alias db-stop="stop_all_db"

# 当加载此文件时显示数据库配置已加载
log_success "数据库环境已加载"
